<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加实训装置' : '编辑实训装置'" :visible.sync="addEditIsDialog_c" :close-on-click-modal="false" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="12">
            <el-form-item label="考场名称：" prop="examinationRoomId">
              <el-select v-model="ruleForm.examinationRoomId" placeholder="请选择考场名称" clearable @change="schoolChange" :disabled="seeType ">
                <el-option v-for="item in schoolArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备图片：" prop="image">
              <UploadImg para="image" @setImg="setImg" :imgUrl="ruleForm.image" width="100" height="100"></UploadImg>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编码：" prop="code">
              <el-input v-model="ruleForm.code" placeholder="请输入设备编码" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名称：" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入设备名称" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="24">
            <el-form-item label="网关编码：" prop="gatewaySn">
              <el-input v-model="ruleForm.gatewaySn" placeholder="请输入网关编码" clearable maxlength="100" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>

          <!-- <el-col :span="12">
            <el-form-item label="设备流程图：" prop="flowChartImage">
              <UploadFlowChartImage para="flowChartImage" @setImg="setFlowChartImage" :imgUrl="ruleForm.flowChartImage" width="200" height="100"></UploadFlowChartImage>
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备描述：" prop="remark">
              <el-input v-model="ruleForm.remark" placeholder="请输入设备描述" clearable maxlength="100" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产厂商：" prop="productionBusiness">
              <el-input v-model="ruleForm.productionBusiness" placeholder="请输入生产厂商" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产日期：" prop="productionDate">
              <el-date-picker v-model="ruleForm.productionDate" type="date" placeholder="请选择生产日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd" clearable :disabled="seeType">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序号：" prop="orderNum">
              <el-input-number v-model="ruleForm.orderNum" placeholder="请输入排序号" :precision="0" :step="1" :max="9999" :min="0" :disabled="seeType"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import UploadImg from '@/components/UploadImg.vue'
import UploadFlowChartImage from '@/components/UploadImg.vue'

import { getDeviceInfo, saveDeviceInfo, getDeviceClasss } from '@/api/device'
import { getSchoolInfos } from '@/api/school'

export default {
  components: { UploadImg, UploadFlowChartImage },
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        examinationRoomId: '',
        examinationRoomId: '',
        classId: '',
        code: '',
        name: '',
        gatewaySn: '',
        image: '',
        flowChartImage: '',
        remark: '',
        productionBusiness: '',
        productionDate: '',
        orderNum: ''
      },
      rules: {
        // 表单规则
        examinationRoomId: [{ required: true, message: '请选择考场组织', trigger: 'change' }],
        classId: [{ required: true, message: '请选择设备分类', trigger: 'change' }],
        code: [{ required: true, message: '请输入设备编码', trigger: 'blur' }],
        name: [{ required: true, message: '请输入设备名称', trigger: 'blur' }],
        gatewaySn: [{ required: true, message: '请输入网关编码', trigger: 'blur' }],
        image: [{ required: true, message: '请上传设备图片', trigger: 'change' }],
        flowChartImage: [{ required: true, message: '请上传设备流程图', trigger: 'change' }],
        remark: [{ required: false, message: '请输入设备描述', trigger: 'blur' }],
        productionBusiness: [{ required: false, message: '请输入生产厂商', trigger: 'blur' }],
        productionDate: [{ required: false, message: '请选择生产日期', trigger: 'change' }],
        orderNum: [{ required: true, message: '请输入排序号', trigger: 'blur' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      classArray: [], // 分类集合
      treeId: [], // 考场组织id集合
      schoolArray: [] // 考场组织集合
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = ''
          this.seeType = false
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }

        // 获取考场组织
        this.getSchoolInfos()
      }
    }
  },
  methods: {
    // 详情
    getDetail() {
      getDeviceInfo({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data
          this.treeId = [res.data.examinationRoomId, res.data.collegeId, res.data.facultyId, res.data.floorId, res.data.examinationRoomId]

          // 获取分类列表
          this.getDeviceClassList()
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveDeviceInfo(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.treeId = []
      this.classArray = []
      this.$parent.closeAddEdit()
    },
    // 获取考场组织
    getSchoolInfos() {
      getSchoolInfos().then((res) => {
        if (res.code == 200) {
          this.schoolArray = res.data
        }
      })
    },
    // 考场组织下拉切换
    changeTreeItem(val) {
      // 清空
      this.ruleForm.classId = ''
      this.classArray = []

      let len = val.length
      if (val && len == 5) {
        this.ruleForm.examinationRoomId = val[len - 1]
        this.ruleForm.examinationRoomId = val[0]

        // 获取设备分类列表
        this.getDeviceClassList()
      } else {
        this.ruleForm.examinationRoomId = ''
        this.treeId = []
      }
    },
    // 获取设备分类列表
    getDeviceClassList() {
      if (!this.ruleForm.examinationRoomId || this.ruleForm.examinationRoomId == 0) {
        return
      }
      getDeviceClasss({ id: this.ruleForm.examinationRoomId }).then((res) => {
        if (res.code == 200) {
          this.classArray = res.data
        }
      })
    },
    // 上传图片
    setImg(url) {
      this.ruleForm.image = url
    },
    // 上传图片
    setFlowChartImage(url) {
      this.ruleForm.flowChartImage = url
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }

  .el-cascader {
    width: 100%;
  }
}
</style>
